﻿#define DEBUG
using System;
using UnityEngine;
using UnityEngine.UI;
namespace Assets.Scripts.Interface
{
    /// <summary>
    /// 物品的基类。
    /// 在地图中以物品显示。拾取后为玩家道具
    /// </summary>
    [Serializable, RequireComponent(typeof(Collider))]
    public abstract class AProp : MonoBehaviour
    {
        #region field
        [SerializeField, Tooltip("长按提示的信息"),Multiline]
        public string info = "";
        [SerializeField, Tooltip("显示的按钮")]
        protected GameObject PropButton;
        [SerializeField, Tooltip("总的使用次数。-1表示无限次")]
        public int useCount = 5;
        [SerializeField, Tooltip("道具类型")]
        private PropType type;
        [SerializeField, Tooltip("获取道具音效的脚本")]
        private ObtainMusic om;
        private SpriteRenderer sr;//显示的图象
        private Collider co;//碰撞器
        private UIshower uIshower;
        protected bool isEquiping = false;//是否处于被玩家装备状态
        [SerializeField, Tooltip("获取冷却时间")]
        public float cdTime = 0.5f;
        #endregion
        #region property
    
        public PropType Type
        {
            get
            {
                return type;
            }

            set
            {
                type = value;
            }
        }

        public bool IsEquiping { get { return isEquiping; } }

        #endregion
        #region normalMethod
        /// <summary>
        /// 是否显示按钮
        /// 是则显示，否则隐藏
        /// </summary>
        public void ShowButton(bool isShowBtn)
        {

            if (isShowBtn&&PropButton!=null)
            {
                
                PropButton.gameObject.SetActive(true);
                Transform tranChild = PropButton.transform.GetChild(0);//获取修改sprite的字段
                tranChild.GetComponent<Image>().sprite = sr.sprite;

                PropButton.GetComponent<ButTool>().type = (int)type;
            }
            else if(PropButton != null) PropButton.gameObject.SetActive(false);
        }

        /// <summary>
        /// 长按显示帮助
        /// </summary>
        public void Introduce()
        {
            if (PropButton.transform.GetChild(1).localScale == new Vector3(0, 0, 0))
                UIshower.Instance.ShowFromSize(PropButton.transform.GetChild(1), new Vector3(1.5f, 1.5f, 1.5f), 0.1f, 2f, info);
        }




        /// <summary>
        /// 显示的道具次数，Infinite是无限次
        /// </summary>
        /// <returns></returns>
        public string CountText()
        {
            if (useCount == -1)
            {
                return "Infinite";
            }
            else if (useCount > -1)
            {
                return useCount + "";
            }
            else
            {
                throw new Exception("Prop Count error");//如果既不是-1也不是自然数则有异常
            }
        }

        /// <summary>
        /// 初始化组件。让子类也使用该方法
        /// </summary>
        private void Awake()
        {
            // 
            sr = GetComponentInChildren<SpriteRenderer>();
            co = GetComponent<Collider>();
            this.ProbeComponent(ref sr, "prop don't contain Animation");
            this.ProbeComponent(ref co, "prop don't contain collider");
            // print("co==null:" + (co == null));//debug
            if (useCount < -1)
            {
                print("错误的赋值");
                throw new Exception("Prop Count error");//如果既不是-1也不是自然数则有异常
            }
            FixedAwake();
        }

        public virtual void Start()
        {
            PropButton = PropManager.Instance.propButton[(int)Type];
            om = PropManager.Instance.pvPlayer;
        }
        #endregion

        #region virtualMethod
        /// <summary>
        /// 使用道具减少使用次数到0.
        /// </summary>
        /// <param name="reduce"></param>
        public virtual void ReduceCount(int reduce)
        {
            if (useCount==-1)//如果是-1，无限次使用
            {
                return;
            }
            if (useCount - reduce > 0)
            {
                useCount -= reduce;
                
            }
            else//次数用完
            {
                useCount = 0;
                ShowButton(false);//按钮消失
                PropManager.Instance.Items[(int)type] = null;
                GameObject.Destroy(gameObject);//销毁本身

            }
        }


        /// <summary>
        /// 被玩家拾取的逻辑
        /// 丧失场景道具的功能变成玩家物品
        /// </summary>
        /// <param name="other"></param>
        protected virtual void OnTriggerEnter(Collider other)
        {
            
            if (other.tag.Equals("Player"))//被玩家拾取
            {

                PropManager.Instance.ChangeItem(gameObject.GetComponent<AProp>());
                ShowButton(true);//显示按钮
                if (om != null) om.ChangeClip(om.musicGather[0]);
                co.enabled = false;//不再显示sprite
                sr.enabled = false;//不再使用collider
                isEquiping = true;//状态为已装备
                //z
            }
        }

        #endregion

        #region abstractMethod
        /// <summary>
        /// 使用道具的方法。需要在方法中减少道具使用次数
        /// </summary>
        public abstract void Click();

        /// <summary>
        /// 子类用FiedAwake方法来代替Awake方法
        /// </summary>
        public abstract void FixedAwake(); 
        #endregion
    }


    /// <summary>
    /// 对应三种类型的道具：功能，攻击，防御
    /// </summary>
    public enum PropType
    {
        FUNCTION, ATTACK, DEFENCE
    }

    /// <summary>
    /// 道具id，每个道具唯一对应一个 fixme
    /// </summary>
    public enum PropId
    {
        LazerGun,ANProp,StartPill,JackProp,Jump,Key,turner,Gold
    }
}
    

